package pl.prv.polanie.client.widgets.table;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dialogs.CreateSubgroupDialog;
import pl.prv.polanie.client.dto.CSSubgroupDTO;
import pl.prv.polanie.client.dto.CSVSubgroupDTO;
import pl.prv.polanie.client.dto.ICSDTO;
import pl.prv.polanie.client.dto.ICSVDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ServiceHolder;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.panels.lists.SubgroupsList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

/**
 * Kontrolka tabelki przechowujacej dane o Subgroupsach.
 * 
 * @author Janusz Prokulewicz
 * Created: Jun 10, 2008
 */
public class TableSubgroups extends Composite {
	
	private SubgroupsList parentSubgroupsList = null;
	private long groupID;
	
	private CustomTable mainCT = new CustomTable(){
		@Override
		protected void runOnClickSelectedRow(int row, int column,
				ICSVDTO csvDto, Widget w) {
			super.runOnClickSelectedRow(row, column, csvDto, w);
			
			
			Planner.log("TableLecturers.runOnClickSelectedRow: " + "" + csvDto + "  ------- row " + row + ", col " + column);
			
			CSSubgroupDTO csSubgroupDTO = (CSSubgroupDTO)mainCT.getCSDtoByID(csvDto.getId());
			
			//wyswietlenie okienka do edycji
			CreateSubgroupDialog csd = new CreateSubgroupDialog(groupID, parentSubgroupsList, csSubgroupDTO); 
			csd.center();
		}
		
		@Override
		protected void runOnSelectRow(ICSVDTO csvDto) {
			super.runOnSelectRow(csvDto);
			
			Planner.log("TableLecturers.runOnSelectRow: " + "wybrano wiersz id " + csvDto.getId() );

			if (parentSubgroupsList != null) {
				parentSubgroupsList.showRemoveSelectedRecordButton();
			}
		}
		
	};
	
	public TableSubgroups(long groupID, SubgroupsList subgroupsList) {
		this.groupID = groupID;
		parentSubgroupsList = subgroupsList;
		
		mainCT.setColumnCount(4);
		initWidget(mainCT);
		
		mainCT.addHeaderColumn(" ", 20);
		mainCT.addHeaderColumn("name", 150);
		mainCT.addHeaderColumn("students number", 120);
		mainCT.addHeaderColumn("subgroup type", 130);
	}
	
	
	/**
	 * Dodaje do tabeli 
	 * @param cs
	 * @param csv
	 */
	public void addRow(ICSDTO cs, ICSVDTO csv) {
		mainCT.addRow(cs, csv);
	}
	
	
	public void saveOrUpdateSubgroupInTable(CSSubgroupDTO csDto) {
		CSVSubgroupDTO csvS = ViewDataProvider.getCSVSubgroup(csDto);
		mainCT.addOrUpdateDTOInTable(csDto, csvS);
	}
	
	/**
	 * Wysyla calla do serwera usuwajacego na bazie lecturera i jesli operacja sie uda to usuwa takze z tabelki.
	 * @param csDto
	 */
	public void removeSelectedSubgroupFromTable() {

		final CSVSubgroupDTO csvS = (CSVSubgroupDTO)mainCT.getSelectedCSVDto();
		if (csvS != null) {
			final CSSubgroupDTO csS = (CSSubgroupDTO)mainCT.getCSDtoByID(csvS.getId());
			
			
			
			AsyncCallback callback = new AsyncCallback() {
				public void onFailure(Throwable caught) {
					GWT.log("TableSubgroups.removeSelectedSubgroupFromTable() FAILURE", null);
				}
				public void onSuccess(Object result) {
					Boolean res = (Boolean)result;
					Planner.log("TableSubgroups.removeSelectedSubgroupFromTable() SUCCESS -> zwrocone wynik = " + res +
							", proba usuniecia Subject: " + csS);

					//TODO dodac do kontrolki tabelki
					if (res.booleanValue() == true) {
						if (mainCT != null) {
							mainCT.removeRow(csvS);
						}
						Cache.removeCSSubgroup(csS);
					} else {
						
					}

					//update w cache
				}
			};
			
			ServiceHolder.get().removeCSSubgroup(csS, callback);
		}
		
	}	
	
	
	protected void runOnHighlightingRow(ICSVDTO csvDto) {}

	public void highlightRowByID(long id) {
		mainCT.highlightRowByID(id);
	}
	
	public void setSelectedRowByID(long id) {
		mainCT.setSelectedRowByID(id);
	}
	
	public void removeSelectionFromRow() {
		mainCT.removeSelectionFromSelectedRow();
	}
	
	public void removeHighlightingFromRows() {
		mainCT.removeHighlightingFromRows();
	}
	
}
